//
//  MHSetCircleInfoViewModel.m
//  MiaoCircle
//
//  Created by 许正荣 on 2018/4/17.
//  Copyright © 2018年 acgera. All rights reserved.
//

#import "MHSetCircleInfoViewModel.h"
#import "MLCircleInfoMainPageViewModel.h"
//#import "mhcircleinf"
#import "MHSetCirclePriceViewModel.h"

@interface MHSetCircleInfoViewModel();
@property (nonatomic, strong, readwrite)  MHCircleTagViewModel* tagViewModel;

@property (nonatomic, readwrite, strong) RACCommand *nextCommand;

//@property (nonatomic, readwrite, strong) RACSignal *validCircleSignal;



@end

@implementation MHSetCircleInfoViewModel

- (instancetype)initWithServices:(id<MHViewModelServices>)services params:(NSDictionary *)params{
    if (self = [super initWithServices:services params:params]) {
//        MHCircleInfo *info =  [MHCircleInfo new];
        self.info = params[MHViewModelUtilKey];
        self.isCreate = params[MHViewModelIsCreate];
//        if(self.isCreate)
//        {
//
//        }
        if (self.isCreate) {
            self.info = [MHCircleInfo new];
            self.info.payType = MHCirclPayTypeFree;
            self.info.category = MHCirclContenTypeText;
        }
       
    }
    return self;
}



-(void)initialize
{
     [super initialize];
     self.title = @"设置圈子资料";
//    self.rightTitle = self.isCreate ? @"下一步" : @"完成";
    self.translucentNavigationBar = YES;
    self.prefersNavigationBarBottomLineHidden = YES;
    self.tagViewModel  = [[MHCircleTagViewModel alloc] initWithServices:self.services params:nil];
     @weakify(self);
    void (^doNext)(MHCircleInfo *) = ^(MHCircleInfo *info){
        @strongify(self);
        if (info && self.isCreate ) {
           [MHMomentHelper redirectToGroupChatWithCircleId:info.autoId service:self.services groupId:info.circleCode title:info.title];
           
//            MLCircleInfoMainPageViewModel *viewModel = [[MLCircleInfoMainPageViewModel alloc] initWithServices:self.services params:@{MHViewModelUtilKey: info,MHViewModelIDKey:@(info.autoId),@"MHViewModelFromCreate":@(true)}];
//            [self.services pushViewModel:viewModel animated:YES];
        }else
        {
            [self.services popViewModelAnimated:YES];
        }
    };
    
    self.nextCommand =[[RACCommand alloc] initWithSignalBlock:^RACSignal *(NSIndexPath * indexPath) {
        
            @strongify(self);
//        self.info.location
        if (self.isCreate) {
            if (self.info.joinPrice > 0) {
                self.info.payType = MHCirclPayTypeCharge;
            }
    
            return  [[[self.services.client createCircleWith:self.info] mh_parsedResults] doNext:doNext];
        }else
        {
            //权限配置
            if([self.info currentUserIsOwner])
            {
                return  [[[self.services.client updateCircleInfoWithInfo:self.info] mh_parsedResults] doNext:doNext];
            }else
            {
                [self.services popViewModelAnimated:YES];
                return [RACSignal empty];
            }
           
        }
        
    }];
    
    
    self.uploadImageCommand = [[RACCommand alloc] initWithSignalBlock:^RACSignal *(id input) {
        @strongify(self);
        self.services.client.swithUrlWithUpload = YES;
        
        return [self.services.client UploadCircleCoverWith:nil  fileDatas:input miniType:@"aa"];
    }];
    
}
@end
